// Playground - noun: a place where people can play

import Cocoa


/*********************************************************************
 * The Swift Language Learning  Chapter 01: The Basics
 *********************************************************************/

/*
 *use keyword as constant or variable
 */
let `let` = "hello world"


/*
 *integers and float point value
 */
let minValue = Int64.min
let maxValue = INT64_MAX

var expressionValue = 3 + 0.1415926

let hexVal = 0xFp-2 //15*2exp(-2)
let hexadecimalDouble = 0xC.3p0 // (12+3*16exp(-1)) * 2exp(0)
let hexadecimal = 0xC.3p1 // 12.188 * 2

let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1 //only '_' works


let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one) //compoile error if don't contain 'UInt(16)'


/*
 *bool value
 */
let boolValue = true //change to 1 will compile error 
                     //because if control must follow a bool value or expression or an option value
if boolValue {
}

/*
 *Tupes
 */
let http404error = (404,"file not found")
let (statusCode, statusMessage) = http404error
let outCode = statusCode
let outMessage = statusMessage

let (justTheStatusCode,_) = http404error
let outStatusCode = justTheStatusCode

http404error.0
http404error.1

let http200success = (statusCode: 200, description: "OK")
http200success.statusCode
http200success.description

/*
 *option Value
 */
let possibleNumber = "123" // "hello" is not ok, change to see what happen
let convertNumber = possibleNumber.toInt()

if convertNumber {
    convertNumber //not unwrap, it's a option type
    let co = convertNumber! //forced unwrapping, now co is a int
}
else {
    let co = convertNumber! //forced unwrapping, because in this blacket convertNumber is false, it will faild when unwrapping. console output: "fatal error: Can't unwrap Optional". playground won't work below anymore, in a ios app it will crash.
}


var optionValue:String? = "hello"
//optionValue = nil
let equal = optionValue;

equal //not unwap

if let actualNumber = optionValue{
    optionValue
    actualNumber //already unwap, it's a string/ because if stame do binding
} else {
    optionValue
    println("\(optionValue) could not be converted to an integer") //it will trait as nil is this blankt
}



// test Implicitly unwrapped optionals

//var a:String = "13" withou ! mark, can't set to nil
//a = nil
var implicitly:String! = "hello world"  //implicitly is umwap as string, but it can set to nil like optionValue, not performance as apple docments description!
implicitly = nil //although it can work. don't do this, use a normal option value instead
let varim = implicitly

//assert(varim, "Implicitly unwrapped optionals should have value")
























